import java.sql.Time;
import java.util.*;
import java.util.concurrent.*;


class MyTask{
    public Runnable runnable;
    public long time;
    public MyTask(Runnable runnable,long time){
        this.runnable = runnable;
        this.time = time;
    }
}
public class MyTimer {

    public MyTimer(){
        Thread thread = new Thread(){
            @Override
            public void run() {
                synchronized (this) {
                    while (true) {

                        long currentTime = System.currentTimeMillis();
                        try {
                            MyTask task = blockingQueue.take();
                            while (currentTime > task.time) {
                                 this.wait(currentTime-task.time);
                            }
                            task.runnable.run();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }
            }
        };
        thread.start();
    }
    class MyCompare implements Comparator<MyTask> {
        @Override
        public int compare(MyTask o1, MyTask o2) {
            return (int)(o1.time-o2.time);
        }
    }

   private PriorityBlockingQueue<MyTask> blockingQueue = new PriorityBlockingQueue<>((Collection) new MyCompare());
    public void schedule(Runnable runnable,long time) throws InterruptedException {

        synchronized (this) {
            MyTask myTask = new MyTask(runnable, time + System.currentTimeMillis());
            blockingQueue.put(myTask);
            blockingQueue.notify();
        }
    }
    public static void main(String[] args) {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("11111");
            }
        },3000);
        CopyOnWriteArrayList<Integer> copyOnWriteArrayList =new CopyOnWriteArrayList<>();
        Hashtable<Integer,Integer> hashtable = new Hashtable<>();
        ConcurrentHashMap<Integer,Integer> map = new ConcurrentHashMap<>();
        map.put(1,1);
    }

}
